<!--头部公共部分-->
{include file="common/header" /}
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a><cite>新增课程</cite></a>
      </span>
</div>
<div style="padding:15px;font-size: 12px;">
    <div class="layui-tab">
        <div class="layui-tab-content">
            <div class="layui-tab-item layui-show">
                <form class="layui-form" action="" method="POST">
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span style="color: red;">*</span>名称：</label>
                        <div class="layui-input-inline" style="width: 520px;">
                            <input type="text" id="name" name="course_name"
                                   value="{notempty name='course'}{$course.course_name}{/notempty}"
                                   lay-verify="required" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-input-inline">
                            <div class="layui-form-mid layui-word-aux" id="auto"><i class="layui-icon">&#xe654;自动生成</i>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label"><span style="color: red;">*</span>校区：</label>
                            <div class="layui-input-inline">
                                <select name="campus_id" lay-search="" lay-verify="required">
                                    <option value="">请选校区</option>
                                    {volist name="campus" id="v"}
                                    <option value="{$v.id}">{$v.campus_name}</option>
                                    {/volist}
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">年份：</label>
                            <div class="layui-input-inline">

                                <!--
                                <select name="course_year">
                                    {volist name="data[12]" id="v"}
                                        <option value="{$v.id}" {if $v.id== 59}selected{/if}>{notempty name='year'}{$v['name']}{/notempty}</option>
                                    {/volist}
                                </select>
                                -->
                                <input type="text" name="course_year" value="{notempty name='course'}{$course.course_year}{/notempty}" id="layui_date" autocomplete="off"
                                     class="layui-input">

                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">期段：</label>
                            <div class="layui-input-inline">
                                <select name="course_quarter">
                                    <option value=""></option>
                                    {notempty name='data[21]'}
                                        {volist name="data[21]" id="vo"}
                                            <option value="{$vo.id}"
                                                {notempty name='course'}
                                                    {if condition="$course.course_quarter==$vo.id" } selected{/if}>{$vo.name} {else/} > {$vo.name}
                                                {/notempty}
                                            </option>
                                        {/volist}
                                    {/notempty}
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">年级：</label>
                            <div class="layui-input-inline">
                                <select name="student_grade">
                                    <option value=""></option>
                                    {notempty name='data[1]'}
                                        {volist name="data[1]" id="vo"}
                                            <option value="{$vo.id}"
                                                {notempty name='course'}
                                                    {if condition="$course.student_grade==$vo.id" } selected{/if}>{$vo.name} {else/} >{$vo.name}
                                                {/notempty}
                                            </option>
                                        {/volist}
                                    {/notempty}
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">科目：</label>
                            <div class="layui-input-inline">
                                <select name="subject">
                                    <option value=""></option>
                                    {notempty name='data[10]'}
                                        {volist name="data[10]" id="vo"}
                                            <option value="{$vo.id}"
                                                {notempty name='course' }
                                                    {if condition="$course.subject==$vo.id" } selected{/if}>{$vo.name} {else/} >{$vo.name}
                                                {/notempty}
                                            </option>
                                        {/volist}
                                    {/notempty}
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">类型：</label>
                            <div class="layui-input-inline">
                                <select name="class_type">
                                    <option value=""></option>
                                    {notempty name='data[22]'}
                                    {volist name="data[22]" id="vo"}
                                    <option value="{$vo.id}"
                                            {notempty name='course' }
                                            {if condition="$course.class_type==$vo.id" } selected{/if}>{$vo.name} {else/} >{$vo.name}
                                    {/notempty}
                                    </option>
                                    {/volist}
                                    {/notempty}
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label"><span style="color: red;">*</span>班型：</label>
                            <div class="layui-inline">
                                <!--
                                <input type="radio" name="course_type" lay-filter="course_type"
                                       class="layui-input" value="1" {notempty name='course' }{if
                                       $course.course_type== 1}checked{/if}{/notempty} title="集体班">
                                <input type="radio" name="course_type" lay-filter="course_type"
                                       class="layui-input" value="2" {notempty name='course' }{if
                                       $course.course_type== 2}checked{/if}{/notempty} title="一对一">
                                <input type="radio" name="course_type" lay-filter="course_type"
                                       class="layui-input" value="3" {notempty name='course' }{if
                                       $course.course_type== 3}checked{/if}{/notempty} title="一对多 (即:1对2、1对3等)">
                                -->
                                {volist name="data[9]" id="vo"}
                                <input type="radio" name="course_type" lay-filter="course_type" value="{$vo.id}"  title="{$vo.name}" class="layui-input">
                                {/volist}
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <div class="layui-inline">
                                <label class="layui-form-label"><span style="color: red;">*</span>计费方式：</label>
                                <div class="layui-input-inline">
                                    <select id="cost_type" name="cost_type" lay-filter="cost_type"
                                            lay-verify="required">
                                        {notempty name='data[23]'}
                                            {volist name="data[23]" id="vo"}
                                                <option value="{$vo.id}"
                                                    {notempty name='course' }
                                                        {if condition="$course.cost_type==$vo.id" } selected{/if}>{$vo.name} {else/} >{$vo.name}
                                                    {/notempty}
                                                </option>
                                            {/volist}
                                        {/notempty}
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline kc">
                                <label class="layui-form-label"><span style="color: red;">*</span>课程次数：</label>
                                <div class="layui-input-inline">
                                    <input type="number" name="class_num" min="1"
                                           value="{notempty name='course'}{$course.class_num}{/notempty}"
                                           autocomplete="off" class="layui-input"
                                           placeholder="次数">
                                </div>
                                <div class="layui-form-mid layui-word-aux"
                                     style="font-weight: bold;font-size: 16px;">节
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label"><span style="color: red;">*</span>课程单价：</label>
                                <div class="layui-input-inline">
                                    <input type="number" name="price" min="1"
                                           value="{notempty name='course'}{$course.price}{/notempty}" autocomplete="off"
                                           lay-verify="required" class="layui-input" placeholder="金额">
                                </div>
                                <div class="layui-form-mid layui-word-aux dj"
                                     style="font-weight: bold;font-size: 16px;">元/次
                                </div>
                            </div>
                            <div class="layui-inline sc">
                                <label class="layui-form-label"><span style="color: red;">*</span>时长：</label>
                                <div class="layui-input-inline">
                                    <input type="number" name="class_time" min="1"
                                           value="{notempty name='course'}{$course.class_time}{/notempty}" autocomplete="off"
                                          class="layui-input" placeholder="时长">
                                </div>
                                <div class="layui-form-mid layui-word-aux"
                                     style="font-weight: bold;font-size: 16px;">分钟/次
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">描述：</label>
                        <div class="layui-input-block">
                            <textarea name="describe" style="width: 500px;" placeholder="请输入内容" class="layui-textarea">{notempty name='course'}{$course.describe}{/notempty}</textarea>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <input type="hidden" name="id" value="{notempty name='course'}{$course.id}{/notempty}">
                            <input type="hidden" id="type" value="{notempty name='course'}{$course.cost_type}{/notempty}">
                            <input class="layui-btn" lay-submit lay-filter="wxsubmit" id="sub" value="提交"
                                   readonly>
                            <a class="layui-btn layui-btn-primary" href="{:url('Course/lists')}">返回</a>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<!--底部公共部分-->
{include file="common/footer" /}


<script>
    layui.use(['element', 'jquery', 'form', 'layer', 'laydate'], function () {
        var $ = layui.jquery,
            laydate = layui.laydate,
            element = layui.element,
            layer = layui.layer,
            form = layui.form;
        let type = $('#type').val();
        $(".kc").hide();
        console.log(type);
        if (type == 97) {
            $(".kc").show();
            $(".sc").show();
        } else if (type == 95) {
            $(".kc").hide();
            $(".sc").hide();
        } else if (type == 89) {
            $(".kc").hide();
            $(".sc").show();
        }
        var date=new Date;
        if($('input[name=course_year]').val() == ''){
            year = date.getFullYear();
        }else{
            year =  $('input[name=course_year]').val();

        }

        laydate.render({
            elem: '#layui_date'
            ,type: 'year'
            ,calendar: true
            ,btns: ['now', 'confirm']
            ,value:year
            ,trigger: 'click'
        });

        form.on('select(cost_type)', function (data) {
            let cost_type = data.value;
            console.log(cost_type+''+111111111);
            if (cost_type == 97) {
                $(".dj").text('元/期');
                $(".kc").show();
                $(".sc").show();
            } else if (cost_type == 95) {
                $(".dj").text('元/小时');
                $(".kc").hide();
                $(".sc").hide();
            } else if (cost_type == 89) {
                $(".dj").text('元/课次');
                $(".kc").hide();
                $(".sc").show();
            }
        });

        $('.bd').click(function () {
            var url = "{:url('Course/goods')}";
            layer.open({
                type: 2,
                title: '选择物品',
                shadeClose: true,
                shade: 0.5,
                area: ['95%', '95%'],
                content: url
            });
        });

        $('.sl').hide();
        $('.kx').click(function () {
            var id = $('#kx').val();
            if (id == 1) {
                $('.sl').show();
                $('#kx').val(2);
            } else if (id == 2) {
                $('.sl').hide();
                $('#kx').val(1);
            }
        });

        $('#auto').click(function () {
        //    let course_year = $('select[name=course_year] option:selected').text();
            let course_year = $('input[name=course_year]').val();
            let course_quarter = $('select[name=course_quarter] option:selected').text();
            let subject = $('select[name=subject] option:selected').text();
            let student_grade = $('select[name=student_grade] option:selected').text();
            let course_type = $('select[name=course_type] option:selected').text();
            let class_type = $('select[name=class_type] option:selected').text();
            let html = course_year + course_quarter + student_grade + subject + course_type + class_type;
            let res = html.replace(/\s+/g,"");
            $("#name").val(res);
        });
        //日期
        laydate.render({
            elem: '#start_one',
            type: 'date'
            ,trigger: 'click'
        });
        form.on('submit(wxsubmit)', function (data) {
            var field = data.field;
            field.course_name = $('input[name=course_name]').val();
            field.course_type = $('input[name=course_type]:checked').val();
            if (!field.course_type || field.course_type === '' || field.course_type == null) {
                layer.msg('课程类型不能为空');
                return false;
            }
            var cost_type = $('select[name=cost_type] option:selected').val();
            if(cost_type == 97){
                field.class_num = $('input[name=class_num]').val();
                if(field.class_num == '' || field.class_num.length <= 0){
                    layer.msg('课程次数不能为空');
                    return false;
                }

                field.class_time = $('input[name=class_time]').val();
                if(field.class_time == '' || field.class_time.length <= 0){
                    layer.msg('课程时长不能为空');
                    return false;
                }
            }
            if(cost_type == 89){
                field.class_num = $('input[name=class_num]').val();
                field.class_time = $('input[name=class_time]').val();
                if(field.class_time == '' || field.class_time.length <= 0){
                    layer.msg('课程时长不能为空');
                    return false;
                }
            }
            $.ajax({
                type: 'post',
                url: '{:url("Course/add")}',
                data: field,
                success: function (result) {
                    if (result.code === 1) {
                        layer.msg(result.msg);
                        location.href = "{:url('Course/lists')}";
                    } else {
                        layer.msg(result.msg);
                    }
                }
            });
        });
    });
</script>
